#pragma once

#include "PathFinder.h"

using namespace System;

namespace ZakuPR320 {

	inline void MarshalString ( String ^ s, string& os ) {
		using namespace Runtime::InteropServices;
		const char* chars = 
			(const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
		os = chars;
		Marshal::FreeHGlobal(IntPtr((void*)chars));
	}

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for MainForm
	/// </summary>
	public ref class MainForm : public System::Windows::Forms::Form
	{
	public:
		MainForm(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
			Finder = new PathFinder();
			MyNet = new TrainNet("Net.xml");
			Finder->SetNet(MyNet);

			AutoCompleteStringCollection^ Stations = gcnew AutoCompleteStringCollection();
			for(auto it=MyNet->Stations.begin();it!=MyNet->Stations.end();it++)
			{
				Stations->Add(gcnew String((*it)->m_Name.c_str()));
			}
			this->InitialStation->AutoCompleteCustomSource=Stations;
			this->TargetStation->AutoCompleteCustomSource=Stations;
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~MainForm()
		{
			if (components)
			{
				delete components;
			}

			delete Finder;
			Finder=0;
			delete MyNet;
			MyNet=0;
		}
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::TextBox^  InitialStation;
	protected: 

	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::TextBox^  TargetStation;
	private: System::Windows::Forms::Button^  FindConnection;
	private: System::Windows::Forms::TextBox^  Output;
	private: PathFinder*	Finder;
	private: TrainNet*		MyNet;
	private: System::Windows::Forms::CheckBox^  BiDirectional;


	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->InitialStation = (gcnew System::Windows::Forms::TextBox());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->TargetStation = (gcnew System::Windows::Forms::TextBox());
			this->FindConnection = (gcnew System::Windows::Forms::Button());
			this->Output = (gcnew System::Windows::Forms::TextBox());
			this->BiDirectional = (gcnew System::Windows::Forms::CheckBox());
			this->SuspendLayout();
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(23, 30);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(65, 13);
			this->label1->TabIndex = 0;
			this->label1->Text = L"Start-Station";
			// 
			// InitialStation
			// 
			this->InitialStation->AccessibleName = L"FirstStation";
			this->InitialStation->AutoCompleteMode = System::Windows::Forms::AutoCompleteMode::SuggestAppend;
			this->InitialStation->AutoCompleteSource = System::Windows::Forms::AutoCompleteSource::CustomSource;
			this->InitialStation->Location = System::Drawing::Point(26, 47);
			this->InitialStation->Name = L"InitialStation";
			this->InitialStation->Size = System::Drawing::Size(357, 20);
			this->InitialStation->TabIndex = 1;
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(26, 85);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(55, 13);
			this->label2->TabIndex = 2;
			this->label2->Text = L"Zielstation";
			// 
			// TargetStation
			// 
			this->TargetStation->AccessibleName = L"TargetStation";
			this->TargetStation->AutoCompleteMode = System::Windows::Forms::AutoCompleteMode::SuggestAppend;
			this->TargetStation->AutoCompleteSource = System::Windows::Forms::AutoCompleteSource::CustomSource;
			this->TargetStation->Location = System::Drawing::Point(26, 102);
			this->TargetStation->Name = L"TargetStation";
			this->TargetStation->Size = System::Drawing::Size(357, 20);
			this->TargetStation->TabIndex = 3;
			// 
			// FindConnection
			// 
			this->FindConnection->AccessibleName = L"Find";
			this->FindConnection->Location = System::Drawing::Point(26, 162);
			this->FindConnection->Name = L"FindConnection";
			this->FindConnection->Size = System::Drawing::Size(106, 23);
			this->FindConnection->TabIndex = 4;
			this->FindConnection->Text = L"Verbindung Finden";
			this->FindConnection->UseVisualStyleBackColor = true;
			this->FindConnection->Click += gcnew System::EventHandler(this, &MainForm::button1_Click);
			// 
			// Output
			// 
			this->Output->AccessibleName = L"Output";
			this->Output->Location = System::Drawing::Point(26, 191);
			this->Output->Multiline = true;
			this->Output->Name = L"Output";
			this->Output->ReadOnly = true;
			this->Output->ScrollBars = System::Windows::Forms::ScrollBars::Vertical;
			this->Output->Size = System::Drawing::Size(376, 206);
			this->Output->TabIndex = 5;
			// 
			// BiDirectional
			// 
			this->BiDirectional->AutoSize = true;
			this->BiDirectional->Checked = true;
			this->BiDirectional->CheckState = System::Windows::Forms::CheckState::Checked;
			this->BiDirectional->Location = System::Drawing::Point(26, 129);
			this->BiDirectional->Name = L"BiDirectional";
			this->BiDirectional->Size = System::Drawing::Size(150, 17);
			this->BiDirectional->TabIndex = 6;
			this->BiDirectional->Text = L"Use bidirectional Algorithm";
			this->BiDirectional->UseVisualStyleBackColor = true;
			this->BiDirectional->CheckedChanged += gcnew System::EventHandler(this, &MainForm::checkBox1_CheckedChanged);
			// 
			// MainForm
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(414, 409);
			this->Controls->Add(this->BiDirectional);
			this->Controls->Add(this->Output);
			this->Controls->Add(this->FindConnection);
			this->Controls->Add(this->TargetStation);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->InitialStation);
			this->Controls->Add(this->label1);
			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;
			this->Name = L"MainForm";
			this->Text = L"Verbindungsassistent";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			 string InitialStationName;
			 string TargetStationName;
			 string OutputString="";
			 
			 this->Output->Text="";

			 MarshalString(this->InitialStation->Text,InitialStationName);
			 if(InitialStationName.length()==0)
				 this->Output->Text+="Bitte eine Start-Station eingeben.\r\n\r\n";
			 else if(!MyNet->DoesStationExist(InitialStationName))
				 this->Output->Text+="Start-Station nicht gefunden.\r\n\r\n";
			 MarshalString(this->TargetStation->Text,TargetStationName);
			 if(TargetStationName.length()==0)
				 this->Output->Text+="Bitte eine Ziel-Station eingeben.\r\n\r\n";
			 else if(!MyNet->DoesStationExist(TargetStationName))
				 this->Output->Text+="Ziel-Station nicht gefunden.\r\n\r\n";

			 if(this->Output->Text->Length>0)
				 return;

			 Finder->FindFastestRoute(MyNet->FindStationByName(InitialStationName),MyNet->FindStationByName(TargetStationName));

			 //return connection
			 OutputString=Finder->GetPath();
			 this->Output->Text=gcnew String(OutputString.c_str());
		 }
private: System::Void checkBox1_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
			 Finder->SetBidirectional(this->BiDirectional->Checked);
		 }
};
}
